package net.caidingke.shepherd.domain.common;

import com.google.common.base.Strings;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import net.caidingke.shepherd.utils.constant.Const;

import javax.persistence.Embeddable;
import java.util.List;
import java.util.Optional;

/**
 * 坐标
 *
 * @author bowen.
 */
@Embeddable
@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class Coordinate {

    private Double latitude;

    private Double longitude;

    public static Optional<Coordinate> fromString(String coordinate) {
        Optional<Coordinate> optional = Optional.empty();
        if (!Strings.isNullOrEmpty(coordinate)) {
            try {
                List<String> coordinates = Const.SPLITTER.splitToList(coordinate);
                optional = Optional.of(new Coordinate(Double.parseDouble(coordinates.get(0)), Double.parseDouble(coordinates.get(1))));
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
        }
        return optional;
    }
}
